package com.leejxx.health.mobile.controller;

import com.leejxx.health.common.MessageConst;
import com.leejxx.health.entity.Result;
import com.leejxx.health.mobile.vo.OrderSubmitParam;
import com.leejxx.health.pojo.Member;
import com.leejxx.health.service.MemberService;
import com.leejxx.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author LeeJx
 * @ClassName LoginController
 * @Description TODO
 * @date 2021/1/6 20:00
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/mobile/login")
public class LoginController {

    @DubboReference
    private SmsService smsService;
    @DubboReference
    private MemberService memberService;

    @RequestMapping("/smsLogin")
    public Result smsLogin(@RequestBody OrderSubmitParam orderSubmitParam) {
        log.info("[用户登录-登录信息]phoneNumber:{},validateCode:{}", orderSubmitParam.getTelephone(), orderSubmitParam.getValidateCode());
        if (!smsService.checkValidateCode("MOBILE_SIGNIN", orderSubmitParam.getTelephone(), orderSubmitParam.getValidateCode())) {
            return new Result(false, MessageConst.VALIDATECODE_ERROR);
        }

        Member member = new Member();
        member.setPhoneNumber(orderSubmitParam.getTelephone());
        member = memberService.addIfNotExist(member);

        log.info("[用户登录-登录成功]id:{}", member.getId());
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }
}
